package com.ionehe.group.common.uaa.ex.domain.service.impl;

import com.ionehe.group.common.uaa.ex.domain.service.AdminCacheWriteService;
import com.ionehe.group.common.uaa.ex.dto.ForestNodeMerger;
import com.ionehe.group.common.uaa.ex.dto.FunctionDTO;
import com.ionehe.group.common.uaa.ex.dto.ManageInfoDTO;
import com.ionehe.group.common.uaa.ex.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Copyright (c) 2020 ionehe.com
 * Date: 2020/11/30
 * Time: 4:19 下午
 *
 * @author 2020年 <a href="mailto:a@ionehe.com">秀</a>
 */
@Service
@Slf4j
public class AdminCacheWriteServiceImpl implements AdminCacheWriteService {
    @Autowired
    private RedisUtils redisUtils;
    @Value("${redis.database:base}")
    private String redisDatabase;
    @Value("${redis.expire.common:86400}")
    private Long redisExpire;
    @Value("${redis.key.admin:uaa:admin}")
    private String redisKeyAdmin;
    @Value("${redis.key.resourceList:uaa:resourceList}")
    private String redisKeyResourceList;

    @Override
    public void setAdmin(ManageInfoDTO admin) {
        log.info("AdminCacheWriteService[]setAdmin[]start! admin:{}", admin);
        String key = redisDatabase + ":" + redisKeyAdmin + ":" + admin.getAccount();
        redisUtils.set(0, key, admin, redisExpire);
    }

    @Override
    public void delAdmin(String account) {
        log.info("AdminCacheWriteService[]delAdmin[]start! account:{}", account);
        String key = redisDatabase + ":" + redisKeyAdmin + ":" + account;
        redisUtils.delete(0, key);
    }

    @Override
    public void setResourceList(String account, List<FunctionDTO> resourceList) {
        log.info("AdminCacheWriteService[]setResourceList[]start! account:{}, resourceList:{}", account, resourceList);
        String key = redisDatabase + ":" + redisKeyResourceList + ":" + account;
        redisUtils.set(0, key, ForestNodeMerger.merge(resourceList), redisExpire);
    }

    @Override
    public void delResourceList(String account) {
        log.info("AdminCacheWriteService[]delResourceList[]start! account:{}", account);
        String key = redisDatabase + ":" + redisKeyResourceList + ":" + account;
        redisUtils.delete(0, key);
    }

    /**********************************              service函数分界线             **************************************/

}
